﻿/* Process 工具类
 */

/* 未完事项
 * 1、单元测试。
 */
namespace LSLib.Diagnostics
{
    using System;
    using System.Diagnostics;
    using System.IO;
    using LSLib.Net;

    public class ProcessTools
    {
        /// <summary>常量值：explorer.exe
        /// </summary>
        public const string NAME_Explorer = "explorer.exe";

        /// <summary>打开目录
        /// </summary>
        /// <param name="directory">目录路径</param>
        public static void Open_Directory(string directory)
        {
            if (System.IO.Directory.Exists(directory) == false)
                throw new DirectoryNotFoundException(string.Format("【{0}】不存在！", directory));

            Process.Start(NAME_Explorer, directory);
        }

        /// <summary>打开网站
        /// </summary>
        /// <param name="url">网址</param>
        public static void Open_Website(string url)
        {
            if (url.StartsWith("http://") == false || url.StartsWith("https://") == false) url.Insert(0, "http://");

            if (UrlHelper.CheckOutUrl(url) == false)
                throw new ArgumentException(string.Format("【{0}】无效！", url), "url");

            Process.Start(url);
        }

        /// <summary>常量值：regsvr32.exe
        /// </summary>
        public const string NAME_Regsvr32 = "regsvr32.exe";

        /// <summary>启动 regsvr32.exe
        /// </summary>
        /// <param name="args">动态库(组件)的名称</param>
        /// <param name="isWaitForExit">是否等待进程退出</param>
        public static void Start_Regsvr32(string args, bool isWaitForExit)
        {
            Process regProcess = new Process();
            regProcess.StartInfo.FileName = NAME_Regsvr32;
            regProcess.StartInfo.Arguments = args;
            regProcess.Start();

            if (isWaitForExit) regProcess.WaitForExit();
        }
    }
}
